<?php

namespace Fronts\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class DetailController extends AbstractActionController {

    private $VideosTable;
    private $CatalogTable;

    public function getVideosTable() {
        if (!$this->VideosTable) {
            $pst = $this->getServiceLocator();
            $this->VideosTable = $pst->get("Fronts\Model\VideosTable");
        }
        return $this->VideosTable;
    }

    public function getCatalogTable() {
        if (!$this->CatalogTable) {
            $pst = $this->getServiceLocator();
            $this->CatalogTable = $pst->get("Fronts\Model\CatalogTable");
        }
        return $this->CatalogTable;
    }

    public function indexAction() {
        $this->loadlayout();
        $iP = $_SERVER["REMOTE_ADDR"];
        //echo $_SERVER["REMOTE_ADDR"];

        $linkvideos = addslashes(trim($this->params()->fromRoute("linkvideos")));
        $checkactive = $this->getVideosTable()->checkactivevideos($linkvideos);
        if ($checkactive == false) {
            $this->layout()->setVariable("detaildata", "");
            $data = array();
            $this->layout()->setVariable("namecatalog", "");
            return array("videol" => $data);
        } else {
            $checkIp = $this->getVideosTable()->checkIp($linkvideos, $iP);
            $data = $this->getVideosTable()->loaddetailvideos($linkvideos);
            if ($checkIp) {
                $this->getVideosTable()->countView($data["videosView"], $linkvideos);
            }
            $namecatalog = $this->getcatalogname();
            $namecata = $namecatalog["catalogName"];
            $catalogId = $namecatalog["catalogId"];
            $titlevideos = $data["videoName"];
            $videol = $this->videosll($catalogId, $titlevideos);
            $this->layout()->setVariable("detaildata", $data);
            $this->getServiceLocator()->get('ViewHelperManager')->get('HeadTitle')->set($titlevideos . " - " . $namecata);
            $this->layout()->setVariable("namecatalog", $namecata);
            return array("videol" => $videol);
        }
    }

    private function loadlayout() {
        $this->layout("layout/layoutdetail");
        $menuWidget = $this->forward()->dispatch('Fronts\Controller\Elements', array(
            'action' => 'menu',
        ));
        $linkWidget = $this->forward()->dispatch("Fronts\Controller\Elements", array(
            'action' => 'widgetlink',
        ));
        $this->layout()->setVariable('linkWidget', $linkWidget);
        $this->layout()->setVariable('listcatalog', $menuWidget);
    }

    private function getcatalogname() {
        $catalogLink = addslashes(trim($this->params()->fromRoute("cataloglink")));
        return $this->getCatalogTable()->getnamecatalog($catalogLink);
    }

    private function videosll($catalogId, $titlevideos) {
        return $this->getVideosTable()->loadvideosll($catalogId, $titlevideos);
    }

}
